package com.lubase.metadata.service.impl;

import com.lubase.metadata.service.ZhPercentageService;
import com.lubase.model.DbEntity;
import com.lubase.orm.QueryOption;
import com.lubase.orm.TableFilter;
import com.lubase.orm.exception.WarnCommonException;
import com.lubase.model.DbCollection;
import com.lubase.orm.service.DataAccess;
import com.lubase.orm.util.TypeConverterUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ZhPercentageServiceImpl implements ZhPercentageService {
    @Autowired
    DataAccess dataAccess;

    @SneakyThrows
    @Override
    public void calculateZhPercentageOfTable(String tableId) {
        // 检查参数
        if (tableId == null) {
            throw new WarnCommonException("calculateZhPercentageOfTable: tableId is empty");
        }
        calculateTableZhPercentage(tableId, true);
    }

    private void calculateTableZhPercentage(String tableId, Boolean calcDatabase) {
        QueryOption queryOption = new QueryOption("mt_column");
        queryOption.setTableFilter(new TableFilter("mt_table_id", tableId));
        queryOption.setFixField("merge_name");
        DbCollection coll = dataAccess.queryAllData(queryOption);
        Integer count = 0;
        for (DbEntity entity : coll.getData()) {
            if (containsChinese(TypeConverterUtils.object2String(entity.get("merge_name"), ""))) {
                count++;
            }
        }

        queryOption = new QueryOption("mt_table");
        queryOption.setTableFilter(new TableFilter("id", tableId));
        queryOption.setFixField("zh_percentage,merge_name,ms_database_id");
        DbCollection collTable = dataAccess.queryAllData(queryOption);
        if (!collTable.getData().isEmpty()) {
            DbEntity entity = collTable.getData().get(0);
            if (containsChinese(TypeConverterUtils.object2String(entity.get("merge_name"), ""))) {
                count++;
            }
            entity.put("zh_percentage", (100 * count) / (coll.getData().size() + 1));
            collTable.setEnableTableTrigger(false);
            dataAccess.update(collTable);

            if (calcDatabase) {
                String databaseId = entity.get("ms_database_id").toString();
                calculateZhPercentageOfDatabase(databaseId);
            }

        }
    }

    @SneakyThrows
    @Override
    public void calculateZhPercentageOfDatabase(String databaseId) {
        // 检查参数
        if (databaseId == null) {
            throw new WarnCommonException("calculateZhPercentageOfDatabase: databaseId is empty");
        }

        QueryOption queryOption = new QueryOption("mt_table");
        queryOption.setTableFilter(new TableFilter("ms_database_id", databaseId));
        queryOption.setFixField("id,zh_percentage,merge_name");
        DbCollection collTable = dataAccess.queryAllData(queryOption);
        if (collTable.getData().isEmpty()) {
            return;
        }
        Integer zhCount = 0;
        // 数据库中文名称率 等于所有表的中文名称率之和
        for (DbEntity table : collTable.getData()) {
            zhCount += TypeConverterUtils.object2Integer(table.get("zh_percentage"), 0);
        }

        queryOption = new QueryOption("ms_database");
        queryOption.setTableFilter(new TableFilter("id", databaseId));
        queryOption.setFixField("zh_percentage");
        DbCollection collDatabase = dataAccess.queryAllData(queryOption);
        if (!collDatabase.getData().isEmpty()) {
            DbEntity entity = collDatabase.getData().get(0);
            entity.put("zh_percentage", ((100 * zhCount) / collTable.getData().size()) / 100);
            collDatabase.setEnableTableTrigger(false);
            dataAccess.update(collDatabase);
        }
    }

    @SneakyThrows
    @Override
    public void calculateZhPercentageOfAllTable(String databaseId) {
        // 检查参数
        if (databaseId == null) {
            throw new WarnCommonException("calculateZhPercentageOfDatabase: databaseId is empty");
        }

        QueryOption queryOption = new QueryOption("mt_table");
        queryOption.setTableFilter(new TableFilter("ms_database_id", databaseId));
        queryOption.setFixField("id");
        DbCollection collTable = dataAccess.queryAllData(queryOption);
        for (DbEntity table : collTable.getData()) {
            calculateTableZhPercentage(table.get("id").toString(), false);
        }
        calculateZhPercentageOfDatabase(databaseId);
    }
}
